package ca.biosample.robot;

import ca.biosample.robot.CBSRobotInterface.FluidLevelType;
import ca.biosample.robot.CBSRobotInterface.SourceTubeSize;

public class CBSRobotTestMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		String barcode = null;
		double volume = 0;
		
		// New interface
		CBSRobotInterface robot = new CBSRobotInterface();
		try{
			robot.initialize(SourceTubeSize.TEN_MIL, FluidLevelType.FLUID_TYPE_BLOOD);
		} catch (CBSRobotInitException cbsre) {
			System.out.println(cbsre);
			System.exit(1);
		} catch (CBSRobotException cbse) {
			System.out.println(cbse);
			System.exit(1);
		}
		// Get tube
		try{
			robot.pickupSourceTube(1);
		} catch(CBSRobotException cbsre) {
			System.out.println(cbsre);
			done(robot);
			System.exit(1);
		}
		// Read barcode
		try{
			barcode = robot.scanSourceTubeBarcode();
		} catch(CBSRobotException cbsre) {
			System.out.println(cbsre);
			done(robot);
			System.exit(1);
		}
		if(barcode == "") {
			System.out.println("Couldn't read source tube barcode");
			done(robot);
			System.exit(1);
		}
		// Get fluid level
		try{
			volume = robot.scanSourceTubeLevel();
		} catch(CBSRobotException cbsre) {
			System.out.println(cbsre);
			done(robot);
			System.exit(1);
		}
		// Grip source tube
		try{
			robot.gripSourceTube();
		} catch(CBSRobotException cbsre) {
			System.out.println(cbsre);
			done(robot);
			System.exit(1);
		}
		// Get pipette tip
		try{
			robot.newPipetteTip();
		} catch(CBSRobotException cbsre) {
			System.out.println(cbsre);
			done(robot);
			System.exit(1);
		}
		int sampleID = 1;
		// Whittle down most of the liquid
		while(volume > 2.0) {
			// Get some liquid
			try{
				volume = robot.aspirateSample(0.8);
			} catch(CBSRobotException cbsre) {
				System.out.println(cbsre);
				done(robot);
				System.exit(1);
			}
			// Dispense to samples
			try{
				robot.dispensePallet(1, sampleID++, 0.4);
				robot.dispensePallet(1, sampleID++, 0.4);
			} catch(CBSRobotException cbsre) {
				System.out.println(cbsre);
				done(robot);
				System.exit(1);
			}
		}
		// Clean up
		try{
			robot.disposePipetteTip();
			robot.returnSourceTube();
			robot.homeArm();
		} catch(CBSRobotException cbsre) {
			System.out.println(cbsre);
			done(robot);
			System.exit(1);
		}
		System.out.println("Success!");			
	}

	public static void done(CBSRobotInterface robot) {
		try {
			robot.allDone();
		} catch (CBSRobotException cbse) {
			System.out.println(cbse);
			System.out.println("Could not cleanly shutdown robot!");
			System.exit(1);
		}
		
	}

}
